package com.tianyang.yukuaifu.business.trade.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.trade.bean.AchievementOutput;
import com.tianyang.yukuaifu.business.trade.contract.AchievementContract;
import com.tianyang.yukuaifu.business.trade.presenter.AchievementPresenter;
import com.tianyang.yukuaifu.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.module.dialog.AchievementDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IAchieveListener;
import com.tianyang.yukuaifu.module.roundview.RoundTextView;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifu.utils.DateUtils;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：业绩
 * Created by xyj on 2024/3/25
 */
public class AchievementActivity extends BaseTitleActivity implements AchievementContract.View {

    @BindView(R.id.subTradeAmountTv)
    TextView subTradeAmountTv;
    @BindView(R.id.subProfitTv)
    TextView subProfitTv;
    @BindView(R.id.directSubAgentNumberTv)
    TextView directSubAgentNumberTv;
    @BindView(R.id.indirectSubAgentNumberTv)
    TextView indirectSubAgentNumberTv;
    @BindView(R.id.directSubMerchantNumberTv)
    TextView directSubMerchantNumberTv;
    @BindView(R.id.indirectSubMerchantNumberTv)
    TextView indirectSubMerchantNumberTv;

    @BindView(R.id.tradeAmountTv)
    TextView tradeAmountTv;
    @BindView(R.id.profitTv)
    TextView profitTv;
    @BindView(R.id.merchantNumberTv)
    TextView merchantNumberTv;

    @BindView(R.id.subTradeAmountsTv)
    TextView subTradeAmountsTv;
    @BindView(R.id.subProfitsTv)
    TextView subProfitsTv;
    @BindView(R.id.subAgentNumberTv)
    TextView subAgentNumberTv;
    @BindView(R.id.subMerchantNumberTv)
    TextView subMerchantNumberTv;

    @BindView(R.id.filterTv)
    TextView filterTv;
    @BindView(R.id.dayFilterRtv)
    RoundTextView dayFilterRtv;
    @BindView(R.id.monthFilterRtv)
    RoundTextView monthFilterRtv;

    private AchievementDialog vAchievementDialog;

    private DateCheckPopListener mDatePopListener;

    private AchievementContract.Presenter mPresenter;

    private boolean isMonthFilter = false;

    private String startTime, endTime;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_achievement;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("业绩");
        mPresenter = new AchievementPresenter(this);
        setTimeData(DateUtils.getTimeAgo(30, "yyyy-MM-dd"), DateUtils.getCurrentTime("yyyy-MM-dd"));
    }

    @OnClick({R.id.filterTv, R.id.dayFilterRtv, R.id.monthFilterRtv, R.id.merchantAchieveRll, R.id.subMerchantRll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.filterTv:
                if (isMonthFilter) {
                    if (mDatePopListener == null) {
                        mDatePopListener = new DateCheckPopListener((Activity) mContext, true);
                    }
                    mDatePopListener.handleRegionCheck(view, "请选择月份", false, new IWheelDataChangeCallback() {
                        @Override
                        public void wheelDataChangeCallback(String wheelData) {
                            setTimeData(wheelData, wheelData);
                        }
                    });
                } else {
                    if (vAchievementDialog == null) {
                        vAchievementDialog = new AchievementDialog(mContext);
                        vAchievementDialog.setListener(new IAchieveListener() {
                            @Override
                            public void callback(String startTime, String endTime) {
                                setTimeData(startTime, endTime);
                            }
                        });
                    }
                    vAchievementDialog.setTimeData(startTime, endTime);
                    vAchievementDialog.show();
                }
                break;
            case R.id.dayFilterRtv:
                if (isMonthFilter) {
                    isMonthFilter = false;
                    showDayOrMonthView();
                }
                break;
            case R.id.monthFilterRtv:
                if (!isMonthFilter) {
                    isMonthFilter = true;
                    showDayOrMonthView();
                }
                break;
            case R.id.merchantAchieveRll:
                TradeOpenProvider.getInstance().openMerchantAchieveActivity(mContext, false);
                break;
            case R.id.subMerchantRll:
                TradeOpenProvider.getInstance().openMerchantAchieveActivity(mContext, true);
                break;
        }
    }

    private void showDayOrMonthView() {
        if (isMonthFilter) {
            dayFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
            monthFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
            setTimeData(DateUtils.getCurrentMonth(), DateUtils.getCurrentMonth());
        } else {
            monthFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
            dayFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
            setTimeData(DateUtils.getTimeAgo(30, "yyyy-MM-dd"), DateUtils.getCurrentTime("yyyy-MM-dd"));
        }
    }

    @Override
    public void showAgentStatView(AchievementOutput output) {
        if (output != null) {
            JSONObject totalTeamResult = output.getTotalTeamResult();
            if (totalTeamResult != null) {
                if (totalTeamResult.containsKey("subTradeAmount")) {
                    subTradeAmountTv.setText(VerifyUtil.decimalFormat(totalTeamResult.getString("subTradeAmount")));
                }
                if (totalTeamResult.containsKey("subProfit")) {
                    subProfitTv.setText(VerifyUtil.decimalFormat(totalTeamResult.getString("subProfit")));
                }
                if (totalTeamResult.containsKey("directSubAgentNumber")) {
                    directSubAgentNumberTv.setText(VerifyUtil.decimalFormat(totalTeamResult.getString("directSubAgentNumber")));
                }
                if (totalTeamResult.containsKey("indirectSubAgentNumber")) {
                    indirectSubAgentNumberTv.setText(VerifyUtil.decimalFormat(totalTeamResult.getString("indirectSubAgentNumber")));
                }
                if (totalTeamResult.containsKey("directSubMerchantNumber")) {
                    directSubMerchantNumberTv.setText(String.valueOf(totalTeamResult.getInteger("directSubMerchantNumber")));
                }
                if (totalTeamResult.containsKey("indirectSubMerchantNumber")) {
                    indirectSubMerchantNumberTv.setText(String.valueOf(totalTeamResult.getInteger("indirectSubMerchantNumber")));
                }
            }
            JSONObject agentResult = output.getAgentResult();
            if (agentResult != null) {
                if (agentResult.containsKey("tradeAmount")) {
                    tradeAmountTv.setText(VerifyUtil.decimalFormat(agentResult.getString("tradeAmount")));
                }
                if (agentResult.containsKey("profit")) {
                    profitTv.setText(VerifyUtil.decimalFormat(agentResult.getString("profit")));
                }
                if (agentResult.containsKey("merchantNumber")) {
                    merchantNumberTv.setText(String.valueOf(agentResult.getInteger("merchantNumber")));
                }
            }
            JSONObject subResult = output.getSubResult();
            if (subResult != null) {
                if (subResult.containsKey("subTradeAmount")) {
                    subTradeAmountsTv.setText(VerifyUtil.decimalFormat(subResult.getString("subTradeAmount")));
                }
                if (subResult.containsKey("subProfit")) {
                    subProfitsTv.setText(VerifyUtil.decimalFormat(subResult.getString("subProfit")));
                }
                if (subResult.containsKey("subAgentNumber")) {
                    subAgentNumberTv.setText(String.valueOf(subResult.getInteger("subAgentNumber")));
                }
                if (subResult.containsKey("subMerchantNumber")) {
                    subMerchantNumberTv.setText(String.valueOf(subResult.getInteger("subMerchantNumber")));
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private void setTimeData(String startTimeStr, String endTimeStr) {
        startTime = startTimeStr;
        endTime = endTimeStr;
        if (isMonthFilter) {
            filterTv.setText(startTime.replaceAll("-", "/"));
        } else {
            filterTv.setText(startTime.replaceAll("-", "/") + "-" + endTime.replaceAll("-", "/"));
        }
        mPresenter.getAgentStatHistory(startTime, endTime);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(AchievementContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (vAchievementDialog != null && vAchievementDialog.isShowing()) {
            vAchievementDialog.dismiss();
            vAchievementDialog = null;
        }
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
